package com.hlt.controller.admin;

import com.hlt.dto.SensitivePostQueryDTO;
import com.hlt.dto.SensitiveWordCheckDTO;
import com.hlt.result.PageResult;
import com.hlt.result.Result;
import com.hlt.service.IPostsService;
import com.hlt.utils.SensitiveWordUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/admin/sensitive-word")
@Api(tags = "敏感词检测接口")
@Slf4j
public class SensitiveWordController {

    @Autowired
    private SensitiveWordUtil sensitiveWordUtil;
    @Autowired
    private IPostsService postsService;

    @PostMapping("/check")
    @ApiOperation("检测文本中的敏感词")
    public Result<SensitiveWordCheckDTO> checkSensitiveWords(@Valid @RequestBody SensitiveWordCheckDTO checkDTO) {
        log.info("检测文本中的敏感词：{}", checkDTO.getContent());

        // 检测是否包含敏感词
        boolean contains = sensitiveWordUtil.contains(checkDTO.getContent());
        checkDTO.setContainsSensitiveWord(contains);

        // 如果包含敏感词，获取所有敏感词
        if (contains) {
            List<String> sensitiveWords = sensitiveWordUtil.findAll(checkDTO.getContent());
            checkDTO.setSensitiveWords(sensitiveWords);
        }

        return Result.success(checkDTO);
    }

    @PostMapping("/sensitive")
    @ApiOperation("查询包含敏感词的文章")
    public Result querySensitivePosts(@RequestBody SensitivePostQueryDTO queryDTO) {
        log.info("查询包含敏感词的文章：{}", queryDTO);
        PageResult pageResult = postsService.querySensitivePosts(queryDTO);
        return Result.success(pageResult);
    }
}